/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package components;

import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import SRDException.*;
import commands.dcliComandos;
import java.util.Date;

/**
 *
 * @author ylopez
 */
public class dcli {
    private static final int PORTDREP = 43718;

    private static int evaluateArgument(String [] args){
        int argumentConfig = 0;
        if (args.length == 10) {
            if (
                args[0].equals("-h") && args[2].equals("-p") && args[4].equals("-d") &&
                args[6].equals("-a") && args[8].equals("-n")
               ) {
                argumentConfig = 101;
            }
        } else if (args.length == 8) {
            if (
                 args[0].equals("-p") && args[2].equals("-d") && args[4].equals("-a") &&
                 args[6].equals("-n")
               ) {
                argumentConfig = 81;
            } else if (
                    args[0].equals("-h") && args[2].equals("-d") && args[4].equals("-a") &&
                    args[6].equals("-n")
                      ){
                argumentConfig = 82;
            } else if (
                    args[0].equals("-h") && args[2].equals("-p") && args[4].equals("-a") &&
                    args[6].equals("-n")
                      ){
                argumentConfig = 83;
            } else if (
                    args[0].equals("-h") && args[2].equals("-p") && args[4].equals("-d") &&
                    args[6].equals("-n")
                      ) {
                argumentConfig = 84;
            } else if (
                    args[0].equals("-h") && args[2].equals("-p") && args[4].equals("-d") &&
                    args[6].equals("-a")
                      ) {
                argumentConfig = 85;
            }
        } else if (args.length == 6) {
            if (args[0].equals("-d") && args[2].equals("-a") && args[4].equals("-n")) {
                argumentConfig = 61;
            } else if (args[0].equals("-p") && args[2].equals("-a") && args[4].equals("-n")) {
                argumentConfig = 62;
            } else if (args[0].equals("-p") && args[2].equals("-d") && args[4].equals("-n")) {
                argumentConfig = 63;
            } else if (args[0].equals("-p") && args[2].equals("-d") && args[4].equals("-a")) {
                argumentConfig = 64;
            } else if (args[0].equals("-h") && args[2].equals("-a") && args[4].equals("-n")) {
                argumentConfig = 65;
            } else if (args[0].equals("-h") && args[2].equals("-d") && args[4].equals("-n")) {
                argumentConfig = 66;
            } else if (args[0].equals("-h") && args[2].equals("-d") && args[4].equals("-a")) {
                argumentConfig = 67;
            } else if (args[0].equals("-h") && args[2].equals("-p") && args[4].equals("-n")) {
                argumentConfig = 68;
            } else if (args[0].equals("-h") && args[2].equals("-p") && args[4].equals("-a")) {
                argumentConfig = 69;
            } else if (args[0].equals("-h") && args[2].equals("-p") && args[4].equals("-d")) {
                argumentConfig = 610;
            }
        } else if (args.length == 4) {
            if (args[0].equals("-h") && args[2].equals("-p")) {
                argumentConfig = 41;
            } else if (args[0].equals("-h") && args[2].equals("-d")) {
                argumentConfig = 42;
            } else if (args[0].equals("-h") && args[2].equals("-a")) {
                argumentConfig = 43;
            } else if (args[0].equals("-h") && args[2].equals("-n")) {
                argumentConfig = 44;
            } else if (args[0].equals("-p") && args[2].equals("-d")) {
                argumentConfig = 45;
            } else if (args[0].equals("-p") && args[2].equals("-a")) {
                argumentConfig = 46;
            } else if (args[0].equals("-p") && args[2].equals("-n")) {
                argumentConfig = 47;
            } else if (args[0].equals("-d") && args[2].equals("-a")) {
                argumentConfig = 48;
            } else if (args[0].equals("-d") && args[2].equals("-n")) {
                argumentConfig = 49;
            } else if (args[0].equals("-a") && args[2].equals("-n")) {
                argumentConfig = 410;
            }
        } else if (args.length == 1) {
            if (args[0].equals("-h")) {
                argumentConfig = 11;
            } else if (args[0].equals("-p")) {
                argumentConfig = 12;
            } else if (args[0].equals("-d")) {
                argumentConfig = 13;
            } else if (args[0].equals("-a")) {
                argumentConfig = 14;
            } else if (args[0].equals("-n")) {
                argumentConfig = 15;
            }
        }

        return argumentConfig;
    }

    public static void main(String [] args){
        String hostDrep = "127.0.0.1";
        int portDrep = PORTDREP;
        String directory = System.getProperty("user.dir");
        String file = null;
        String userName = System.getProperty("user.name");
        System.out.println(directory + " " + userName);

        InetAddress inetAddDrep = null;
        Socket socket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        BufferedReader stdIn = null;

        String fromServer;
        String fromUser;

        try {
            switch (evaluateArgument(args)) {
                case 0:
                    break;
                case 11:
                    hostDrep = args[1]; break;
                case 12:
                    portDrep = Integer.parseInt(args[1]); break;
                case 13:
                    directory = args[1]; break;
                case 14:
                    file = args[1]; break;
                case 15:
                    userName = args[1]; break;
                case 41:
                    hostDrep = args[1]; portDrep = Integer.parseInt(args[3]); break;
                case 42:
                    hostDrep = args[1]; directory = args[3]; break;
                case 43:
                    hostDrep = args[1]; file = args[3]; break;
                case 44:
                    hostDrep = args[1]; userName = args[3]; break;
                case 45:
                    portDrep = Integer.parseInt(args[1]); directory = args[3]; break;
                case 46:
                    portDrep = Integer.parseInt(args[1]); file = args[3]; break;
                case 47:
                    portDrep = Integer.parseInt(args[1]); userName = args[3]; break;
                case 48:
                    directory = args[1]; file = args[3]; break;
                case 49:
                    directory = args[1]; userName = args[3]; break;
                case 61:
                    directory = args[1]; file = args[3]; userName = args[5]; break;
                case 62:
                    portDrep = Integer.parseInt(args[1]); file = args[3]; userName = args[5]; break;
                case 63:
                    portDrep = Integer.parseInt(args[1]); directory = args[3]; userName = args[5]; break;
                case 64:
                    portDrep = Integer.parseInt(args[1]); directory = args[3]; file = args[5]; break;
                case 65:
                    hostDrep = args[1]; file = args[3]; userName = args[5]; break;
                case 66:
                    hostDrep = args[1]; directory = args[3]; userName = args[5]; break;
                case 67:
                    hostDrep = args[1]; directory = args[3]; file = args[5]; break;
                case 68:
                    hostDrep = args[1]; portDrep = Integer.parseInt(args[3]); userName = args[5]; break;
                case 69:
                    hostDrep = args[1]; portDrep = Integer.parseInt(args[3]); file = args[5]; break;
                case 81:
                    portDrep = Integer.parseInt(args[1]); directory = args[3]; file = args[5];
                    userName = args[7]; break;
                case 82:
                    hostDrep = args[1]; directory = args[3]; file = args[5];
                    userName = args[7]; break;
                case 83:
                    hostDrep = args[1]; portDrep = Integer.parseInt(args[3]); file = args[5];
                    userName = args[7]; break;
                case 84:
                    hostDrep = args[1]; portDrep = Integer.parseInt(args[3]); directory = args[5];
                    userName = args[7]; break;
                case 85:
                    hostDrep = args[1]; portDrep = Integer.parseInt(args[3]); directory = args[5];
                    file = args[7]; break;
                case 101:
                    hostDrep = args[1]; portDrep = Integer.parseInt(args[3]); directory = args[5];
                    file = args[7]; userName = args[9]; break;
                case 410:
                    file = args[1]; userName = args[3]; break;
                case 610:
                    hostDrep = args[1]; portDrep = Integer.parseInt(args[3]); directory = args[5];
                    break;
            }
        } catch (NumberFormatException nfe) {
            //TODO Manejar esta excepcion bien
            System.err.println("Alguno de los 2 puertos no es un nu'mero entero.");
            nfe.printStackTrace();
            System.exit(-1);
        }

        try {
            inetAddDrep = InetAddress.getByName(hostDrep);
            socket = new Socket(inetAddDrep, portDrep);
            out = new PrintWriter(socket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("La direcci'on " + inetAddDrep.getHostAddress() +
                    ": " + inetAddDrep.getHostName() + " no se encuentra");
            System.exit(1);
        } catch (IOException e) {
            System.err.println(dcli.class.getName() + ": Error de E/S esperando coneccion de clientes");
            System.exit(1);
        }

        if (file != null) {
            commands.dcliComandos.readCommandsFromFile(file, directory, out, in, socket);
        }

        String[] connectParameter = new String[]{userName,(new Date()).toString()};
        try {
            dcliComandos.connect(connectParameter, out, in);
        } catch (SRDException ex) {
            ex.printException();
            ex.printStackTrace();
            System.exit(-1);
        }

        System.out.println("Escriba Bye para salir");
        System.out.print(userName + "$> ");
        stdIn = new BufferedReader(new InputStreamReader(System.in));
        String[] commandLine;
        String[] resoults;
        try {
            fromUser = stdIn.readLine();
            while (fromUser != null) {
                commandLine = fromUser.split(" ");
                if (commandLine[0].equals("ldir")) {
                    resoults = dcliComandos.ldir(directory);
                    dcliComandos.printResoults("ldir", resoults);
                } else if (commandLine[0].equals("rdir")) {
                    resoults = dcliComandos.rdir(out,in);
                    dcliComandos.printResoults("rdir", resoults);
                } else if (commandLine[0].equals("tuser")) {
                    resoults = dcliComandos.tuser(out,in);
                    dcliComandos.printResoults("tuser", resoults);
                } else if (commandLine[0].equals("nrep")) {
                    resoults = dcliComandos.nrep(out,in);
                    dcliComandos.printResoults("nrep", resoults);
                } else if (commandLine[0].equals("sube")) {
                     resoults = dcliComandos.sube(commandLine[1], out, in, directory, socket);
                     dcliComandos.printResoults("sube", resoults);
                } else if (commandLine[0].equals("baja")) {
                    resoults = dcliComandos.baja(commandLine[1], out, in, directory, socket);
                    dcliComandos.printResoults("baja", resoults);
                } else if (commandLine[0].equals("cat")) {
                    dcliComandos.cat(commandLine[1]);
                } else if (commandLine[0].equals("Bye")) {
                    dcliComandos.disconnect(out, in, connectParameter);
                    break;
                } else {
                    SRDException srde = new SRDException("Comando: " + fromUser
                            + " invalido.");
                    srde.printException();
                }
                System.out.print(userName + "$> ");
                fromUser = stdIn.readLine();
            }
            out.close();
            in.close();
            stdIn.close();
            socket.close();
        } catch (IOException ex) {
            Logger.getLogger(dcli.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
